博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快应用-Canvas实现尺子左右滑动的效果
阅读量:3904 次
发布时间:2019-05-23

本文共 3661 字,大约阅读时间需要 12 分钟。

完成效果:

在这里插入图片描述
在web页面中,如果我们要实现左右滑动的效果,只需要在css中写上:
overflow: hidden;
overflow-x: scroll;
就可以达到效果。但是在快应用中,不支持overflow命令。

所以这里用canvas画图来实现:监听手指左右滑动的事件,手指每滑动一次,重新绘制一次尺子 从而给人视觉上滑动的效果。

核心代码:

template

{
{shortDate==today?'今天':shortDate.substr(0,4)+"年"+shortDate.substr(4,2)+"月"+shortDate.substr(6,2)+"日"}}
{
{foodInfo.FoodName}}
{
{foodInfo.Calory}}千卡/{
{foodInfo.Weight}}{
{foodInfo.UnitName}}
{
{(weight_initParams.count*(weight_initParams.minScale)).toFixed(1)}}
{
{activeUnitInfo.UnitName}}
约{
{((activeUnitInfo.Calory/activeUnitInfo.Amount)*(weight_initParams.count*(weight_initParams.minScale))).toFixed(1)}}千卡 {
{((activeUnitInfo.Weight/activeUnitInfo.Amount)*(weight_initParams.count*(weight_initParams.minScale))).toFixed(1)}}{
{foodInfo.UnitName}}
{
{$item.UnitName}}

Css:

.cover{
width:100%;height: 100%;background-color: #000000;position: fixed;top: 0px;left: 0px;opacity: 0.7}.Choice_count{
width: 100%;height:840px;position: fixed;bottom: 0;left: 0;background-color: #ffffff;flex-direction: column;border-top-left-radius:20px;border-top-right-radius:20px}.date_title{
width:100%;height:120px;border-bottom: 1px solid #EAE5DD;align-items: center;} .dateText{
font-size:36px;color: #473B1D;position:absolute;} .date{
width:100%;height: 120px;position: relative;align-items: center;justify-content: center;} .foodUnit{
width:140px;align-items: center;justify-content: center;} .foodUnit text{
lines:1;text-overflow:ellipsis;} .button{
background-color: #FFD359;width:100%;height: 105px;font-size: 32px;color: #473B1D;font-weight: bold;position:absolute;bottom: 0;left: 0;} .unitList{
align-items:center;justify-content: center;} .unit{
padding:10px,0;color:#473b1d;font-size: 32px;} .unitActive{
padding:10px,0;color:#ffd359;font-weight:bold;font-size: 32px;} .amount_module text{
color:#473b1d;font-weight:700} .amount_module{
width:100%;justify-content:center;align-items:flex-end;font-family:'Microsoft YaHei';} .rule_module{
width:100%;background-color:#fff;flex-direction:column;align-items: center;} .amount{
font-size:72px;font-weight:700;position:relative;top:12px;margin-right:6px} .ruler{
margin-top:6px} .new-canvas{
margin-top:20px;height:200px;width:1000px} .food_detail{
align-items: center;height:160px;justify-content:space-between;} .images{
height: 120px;;position: absolute;right:20px;padding:20px;align-items: center} .images image{
height:34px}

script:

代码中

img.src = ‘…/Common/ruler.png’ //加载本地图片
cxt.drawImage(img, (count * -24) + (i * 240), 0); //绘制图片
图片如下:

在这里插入图片描述

在刚开始实现尺子效果时,我并没有用到图片来画,是用canvas的画笔 line 线条来画的尺子,每滑动一次,就用线条绘制整个尺子效果,很耗性能,画面非常卡顿。后来再做优化的时候,尝试通过借助图片来画,可以流畅运行。

这里贴上的代码,是我从项目里摘出来实现尺子效果的核心代码,并不能直接运行。仅供一个实现思路和流程。

转载地址:http://bomen.baihongyu.com/

你可能感兴趣的文章
POJ 2579 Blurred Vision(我的水题之路——四方格平均值)
查看>>
POJ 2591 Set Definition(我的水题之路——又一个丑数)
查看>>
POJ 2606 Rabbit hunt(我的水题之路——斜率最多)
查看>>
POJ 2608 Soundex(我的水题之路——字符值)
查看>>
POJ 2636 Electrical Outlets(我的水题之路——水,电器接头)
查看>>
POJ 2656 Unhappy Jinjin(我的水题之路——不开心的学习日)
查看>>
POJ 2664 Prerequisites?(我的水题之路——选课)
查看>>
POJ 2665 Trees(我的水题之路——移树,POJ100题啦!)
查看>>
POJ 2840 Big Clock(我的水题之路——水,钟)
查看>>
POJ 2864 Pascal Library(我的水题之路——都来了么)
查看>>
POJ 2871 A Simple Question of Chemistry(我的水题之路——数列两数之差 )
查看>>
POJ 2909 Goldbach's Conjecture(我的水题之路——任一数为素数对之和)
查看>>
POJ 2924 Gauß in Elementary School(我的水题之路——n到m的连和)
查看>>
POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(我的水题之路——加i个d后的第几个素数)
查看>>
POJ 3030 Nasty Hacks(我的水题之路——比较大小)
查看>>
POJ 3062 Celebrity jeopardy(我的水题之路——原样输出)
查看>>
POJ 3077 Rounders(我的水题之路——高精度四舍五入)
查看>>
POJ 3086 Triangular Sums(我的水题之路——三角数累加)
查看>>
POJ 3096 Surprising Strings(我的水题之路——重点,D-pairs)
查看>>
POJ 3100 Root of the Problem(我的水题之路——取A^N最接近B的A)
查看>>